package cn.nosql.tools;

import com.google.common.primitives.Bytes;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyConvertUtil {

    public static List<Byte> objToListBytes(Object obj)
    {
        byte[] bytes = objToBytes(obj);
        return Bytes.asList(bytes);
    }

    public static Object restoreToObject(List<Byte> lst) {
        byte[] bytes = Bytes.toArray(lst);
        return restore(bytes);
    }

    public static byte[] objToBytes(Object obj) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;

        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(obj);
//            byte[] var3 = byteArrayOutputStream.toByteArray();
//            return var3;
            return byteArrayOutputStream.toByteArray();
        } catch (IOException var17) {
            var17.printStackTrace();
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }
            }

        }

        return null;
    }

    public static Object restore(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;

        try {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object var3 = objectInputStream.readObject();
            return var3;
            //return objectInputStream.readObject();
        } catch (ClassNotFoundException | IOException var17) {
            var17.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

            if (byteArrayInputStream != null) {
                try {
                    byteArrayInputStream.close();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }
            }

        }

        return null;
    }

    /**
     * Date to ByteBuffer
     * 在 key value to ByteBuffer 应用
     * */
    public static ByteBuffer toByteBuffer(final Object obj)
    {
        if (obj != null)
        {
            return ByteBuffer.wrap(objToBytes(obj));
        }
        return null;
    }

    /**
     * ByteBuffer to String
     * 在 key value to ByteBuffer 应用
     * */
    public static Object toObj(final ByteBuffer byteBuffer)
    {
        if (byteBuffer != null) {

            byte[] bytes = new byte[byteBuffer.capacity()];
            byteBuffer.get(bytes, 0, bytes.length);

            return restore(bytes);
        }
        return null;
    }
//    public static Object toObj(final ByteBuffer byteBuffer)
//    {
//        if (byteBuffer != null) {
//            //重置 limit 和postion 值
//           // byteBuffer.flip();
//            //获取buffer中有效大小
//            int len = byteBuffer.limit() - byteBuffer.position();
//
//            byte[] bytes = new byte[len];
//
//            for (int i = 0; i < bytes.length; i++) {
//                bytes[i] = byteBuffer.get();
//
//            }
//
//            return restore(bytes);
//        }
//        return null;
//    }
}





































